/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package flashTest;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

public class Flextesting {
    private WebDriver webDriver;
    private String flshObjectId;

    public Flextesting(WebDriver webDriver, String flshObjectId) {
        this.webDriver = webDriver;
        this.flshObjectId = flshObjectId;
    }

    public String click(final String objectId, final String optionalButtonLabel) {
        return call("doFlexClick", objectId, optionalButtonLabel);
   }

    public String click(final String objectId) {
         return click(objectId, "");
    }

//... Omitted for clarity ...

    public String call(final String functionName, final String... args) {
      final Object result =
            ((JavascriptExecutor)webDriver).executeScript(
                 makeJsFunction(functionName, args),
                 new Object[0]);

         return result != null ? result.toString() : null;
    }

    private String makeJsFunction(final String functionName, final String... args) {
         final StringBuffer functionArgs = new StringBuffer();

        if (args.length > 0) {
            for (int j = 0; j < args.length;j++) {
                if (j > 0) {
                    functionArgs.append(",");
            }
                functionArgs.append(String.format("'%1$s'", args[j]));
         }
        }
        return String.format(
            "return document.%1$s.%2$s(%3$s);",
            flshObjectId,
            functionName,
            functionArgs);
    }
}